/// <reference path="../../Scripts/jquery-1.7.1.js"/>
/// <reference path="../../Scripts/knockout-2.2.0.debug.js"/>

$(function () {
    var viewModel = {
        doorManufacturers: ko.observableArray([]),
        selectedItem: ko.observable(''),
        detailedItem: ko.observable(''),

        addNew: function () {
            var item = newItem();
            
//            archieJs.post(
//                archieJs.baseUrl() + "/api/DoorManufacturers",
//                JSON.stringify(item),
//                function (value) {
//                    viewModel.doorManufacturers.push(item);
//                },
//                "json"
//            );
            

            $.ajax({
                url: archieJs.baseUrl() + "/api/DoorManufacturers",
                data: JSON.stringify(item),
                type: "POST",
                contentType: "application/json;charset=utf-8",
                statusCode: {
                    201: function () {
                        viewModel.doorManufacturers.push(item);
                    },
                    404: function () {

                    }
                }
            });
        },

        save: function () {
            var item = viewModel.selectedItem();
            $.ajax({
                url: archieJs.baseUrl() + "/api/DoorManufacturers/put",
                data: JSON.stringify(item),
                type: "PUT",
                contentType: "application/json;charset=utf-8",
                statusCode: {
                    201: function () {
                    },
                    404: function () {

                    }
                }
            });

//                put("/api/DoorManufacturers/put",
//                    JSON.stringify(item),
//                    function (value) {
//                        alert(value);
//                    },
//                    "application/json;charset=utf-8");
        },

        deleteSelected: function () {
            var item = viewModel.selectedItem();
            archieJs.delete_(archieJs.baseUrl() + "/api/DoorManufacturers/" + item.Id,
                { id: item.Id },
                function (value) {
                    viewModel.doorManufacturers.remove(item);
                },
                'json');
        },

        loadSelected: function () {
            var r = archieJs.baseUrl() + "/api/DoorManufacturers/LoadOneItem";

            jQuery.ajax({
                type: "get",
                cache: false,
                url: r,
                data: { id: viewModel.selectedItem().Id },
                success: function (value) {
                    detailedItem = value;
                },
                dataType: "json"
            });
        },
    };


    function newItem() {
        var item =
            {
                Id: archieJs.guid(),
                Name: "new"
            };
        return item;
    }

    $.getJSON(
        archieJs.baseUrl() + "/api/DoorManufacturers/getall",
        function (data) {
            viewModel.doorManufacturers = ko.observableArray(data);
            ko.applyBindings(viewModel);
        }
    );
});
